package com.zw.pdm.module.production.service;

import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.basic.Org;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.module.basic.repository.OrgRepository;
import com.zw.pdm.module.designtask.repository.DsnProStructRepository;
import com.zw.pdm.module.production.repository.MakePlanRepository;
import com.zw.pdm.module.production.repository.MakeSheetRepository;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.Date;


/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionAddService.java
 * @Description TODO
 * @createTime 2020年10月06日 16:43:00
 */
@Service
@SuppressWarnings("all")
public class ProductionAddService {


    @Autowired
    private MakeSheetRepository makeSheetRepository;
    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private OrgRepository orgRepository;
    @Autowired
    private MakePlanRepository makePlanRepository;
    @Autowired
    private LogService logService;
    @Autowired
    private DsnProStructRepository dsnProStructRepository;

    private String msg = "";
    @Value("${user.upload.path}")
    private String uploadPath;


    /** myf创建制作件 */
    @Transactional(rollbackFor = Exception.class)
    public int saveMake(MakeSheet sheet, String[] drawNo, Integer[] drawNum, String[] drawName,String[] makeType, String[] remark, String[] url,Integer[] orderNum) {
        /** myf判断一个项目一个专业一个部分只能提报一回*/
        int countINt = makeSheetRepository.countBySpecialIdAndPartIdAndProCodeAndIsDel(sheet.getSpecialId(),sheet.getPartId(),sheet.getProCode(),0);
        if(countINt>0){
            return -2;
        }


        sheet = genSheet(sheet);
        for (int i = 0; i < drawNo.length; i++) {
            MakePlan makeplan = new MakePlan();
            makeplan.setDrawNo(drawNo[i]);
            makeplan.setDrawNum(drawNum[i]);
            makeplan.setDrawName(drawName[i]);
            makeplan.setMakeType(makeType[i]);
            if (remark.length > 0) {
                makeplan.setRemark(remark[i]);
            }
            if(orderNum.length>0){
                makeplan.setOrderNum(orderNum[i]*100);
            }
            makeplan.setCreateTime(new Date());
            makeplan.setIsDel(Constant.STATUS_DELETE);
            makeplan.setSheetId(sheet.getId());
            makeplan.setChangeStatus(0);
            makeplan = makePlanRepository.save(makeplan);
            if (url.length > 0 && CommonUtils.isNotEmpty(url[i])) {
                StringBuffer dir = new StringBuffer();
                dir.append(uploadPath).append(sheet.getId()).append("/").append(makeplan.getId()).append("/");
                try {
                    org.apache.commons.io.FileUtils.moveToDirectory(new File(url[i]), new File(dir.toString()), true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                StringBuffer srcPart = new StringBuffer();
                srcPart.append(uploadPath).append(ShiroUtils.getLoginName()).append("/temp/");
                StringBuffer targetPart = new StringBuffer();
                targetPart.append("/files/").append(sheet.getId()).append("/").append(makeplan.getId()).append("/");
                makeplan.setUrl(url[i].replace(srcPart.toString(), targetPart.toString()));
                makeplan.setUploadTime(new Date());
                makePlanRepository.saveAndFlush(makeplan);
            }
        }
        logService.saveLog("保存制作件清单", "保存制作件ID：" + sheet.getId());
        return 1;
    }


    /** myf保存制作件make_sheet表的信息 */
    private MakeSheet genSheet(MakeSheet sheet) {
        //保存原始的部分名称
        sheet.setPart(dsnProStructRepository.getOne(sheet.getPartId()).getProPart());
        //接收标识默认为0
        sheet.setReceiveStatus(0);
        //第一提报人员工编号
        sheet.setCreateCodeOne(ShiroUtils.getLoginName());
        //第一提报人姓名
        sheet.setCreateNameOne(ShiroUtils.getUser().getName());
        //第二提报人姓名
        sheet.setCreateNameTwo(ShiroUtils.getUser().getName());
        //第二提报人员工编号
        sheet.setCreateCodeTwo(ShiroUtils.getLoginName());
        //提报人的部门Id
        sheet.setPostUserId(ShiroUtils.getUserId());
        //部门Id
        sheet.setDeptId(ShiroUtils.getUser().getDept());
        //部门名称
        sheet.setDeptName(ShiroUtils.getUser().getDeptName());
        //科室Id
        sheet.setSectionId(ShiroUtils.getUser().getDeptId());
        //科室名称
        sheet.setSectionName(ShiroUtils.getUser().getBranchName());
        Org dept = orgRepository.getOne(ShiroUtils.getUser().getOrgId());
        if (dept != null) {
            sheet.setPostDept(dept.getName());
        }
        //创建时间
        sheet.setCreateTime(new Date());
        //状态 1=未提交，2=已提交，3=被驳回，4=撤消，5=全部审核通过，6=已确认：默认为：1
        sheet.setStatus(1);
        //表单类型 1=原始，2=变更：默认：1
        sheet.setSheetType(1);
        //是否可以变更  0=否，1=是：默认：0
        sheet.setMayChange(0);
        //删除状态 0：默认， 1：删除：默认：0
         sheet.setIsDel(0);
        //拆分状态  0=未拆分，1=已拆分
        sheet.setRawSplit(0);

        return  makeSheetRepository.save(sheet);
    }


}
